package cn.yjr.vectori.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.yjr.vector.ExperienceActivity;
import cn.yjr.vector.R;
import cn.yjr.vector.a.DaoMaster;
import cn.yjr.vector.a.DaoSession;
import cn.yjr.vector.a.DoctorDao;
import cn.yjr.vector.tools.RegistrationInfo;
import cn.yjr.vectori.adapter.MyRegistrationAdapter.ViewHolder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author vector
 *
 */
public class ExpListAdapter extends BaseAdapter{

	JSONArray exps;
	Context context;
	LayoutInflater inflater;
	JSONObject titles = new JSONObject();
	
	public ExpListAdapter(JSONArray exps, Context context){
		this.exps = exps;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		try {
			titles  = new JSONObject("{\"A\":\"主任医师\",\"B\":\"副主任医师\",\"C\":\"主治医师\",\"D\":\"医师\"}");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public int getCount() {
		return exps.length();
	}

	@Override
	public Object getItem(int position) {
		try {
			return exps.get(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		ViewHolder localViewHolder;
		final int position_ = position;
	    if (paramView == null)
	    {
		      localViewHolder = new ViewHolder();
		      paramView = this.inflater.inflate(R.layout.exp_item, paramViewGroup, false);
		      localViewHolder.base_info_tv = ((TextView)paramView.findViewById(R.id.base_info));
		      localViewHolder.time_tv = ((TextView)paramView.findViewById(R.id.time));
		      localViewHolder.check_box = (CheckBox) paramView.findViewById(R.id.select);
		      localViewHolder.arrow = (ImageView) paramView.findViewById(R.id.arrow);
		      localViewHolder.check_box.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
					ExperienceActivity.checked[position_] = isChecked;
				}
		    	  
		      });
		      if(ExperienceActivity.state == 1){
		    	  localViewHolder.arrow.setVisibility(View.VISIBLE);
		    	  localViewHolder.check_box.setVisibility(View.INVISIBLE);
		      }else{
		    	  localViewHolder.arrow.setVisibility(View.INVISIBLE);
		    	  localViewHolder.check_box.setVisibility(View.VISIBLE); 
		      }
		      paramView.setTag(localViewHolder);
		      JSONObject data;
		      try {
				data = (JSONObject) exps.get(position);
				localViewHolder.base_info_tv.setText(data.getString("hospital") + " " + data.getString("section") + " " + titles.getString(data.getString("title")));
			    localViewHolder.time_tv.setText(data.getString("fromYear") + "年" + data.getString("fromMonth") + "月-" + data.getString("endYear") + "年" + data.getString("endMonth") + "月");
		      } catch (JSONException e) {
				// TODO Auto-generated catch block
		    	  e.printStackTrace();
		      }
	    }else{
	    	localViewHolder = (ViewHolder)paramView.getTag();
	    	if(ExperienceActivity.state == 1){
		    	  localViewHolder.arrow.setVisibility(View.VISIBLE);
		    	  localViewHolder.check_box.setVisibility(View.INVISIBLE);
		    }else{
		    	  localViewHolder.arrow.setVisibility(View.INVISIBLE);
		    	  localViewHolder.check_box.setVisibility(View.VISIBLE); 
		    }
	    }
	    return paramView;
	}

	 class ViewHolder
	  {
	    TextView base_info_tv;
	    TextView time_tv;
	    CheckBox check_box;
	    ImageView arrow;
	    ViewHolder()
	    {
	    }
	  }
}
